﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL;
using Extensions;

namespace MyDashBoardNS
{
    public class SmartLibrary
    {
        public int LibraryRecordId{get;set;}
        public string Keywords { get; set; }
        public string Content { get; set; }
    }

    public class SmartLibraryM
    {
        public string ActionMsg = "";
        public DataView dvData;

        public bool SaveLibraryDetails(SmartLibrary obj)
        {
            bool status = false;
            StringBuilder SQL = new StringBuilder();
            try
            {
                if (obj.LibraryRecordId == 0)
                {
                    SQL.Append(" insert into Library (Keywords,[Content],Active) ");
                    SQL.Append(" values (" + obj.Keywords.ToSQLString() + ", " + obj.Content.ToSQLString()   + ",1)");
                }
                else
                {
                    SQL.Append(" UPDATE Library set Keywords = " + obj.Keywords.ToSQLString() + ",[Content]=" + obj.Content.ToSQLString() + " ");
                    SQL.Append(" WHERE ID= " + obj.LibraryRecordId);
                }

                DAC objDAC = new DAC();
                status = objDAC.Save(SQL.ToString());
                ActionMsg = objDAC.ErrorMsg;
            }
            catch (Exception ex)
            {
                ActionMsg = ex.ToString();
                Logger.Log(ActionMsg);
            }

            return status;
        }

        public bool GetLibraryList(SmartLibrary obj)
        {
            bool status = false;

            try
            {
                StringBuilder SQL = new StringBuilder();
                SQL.Append(" select ID,Keywords,[Content] from Library where Active=1 ");

                if (obj.Keywords.IsNotNullAndEmpty()) {
                    SQL.Append(" and keywords like '%"+obj.Keywords+"%' ");
                }

                DAC objDAC = new DAC();
                if (objDAC.Select(SQL.ToString()))
                {
                    dvData = objDAC.dvData;
                    status = true;
                }

            }
            catch (Exception ex)
            {
                ActionMsg = ex.ToString();
                Logger.Log(ActionMsg);
            }

            return status;
        }
    }

    

}
